package org.medicalinstrument.service;


import net.sf.json.JSONArray;
import org.medicalinstrument.bean.*;
import org.medicalinstrument.dao.CustomerDao;
import org.medicalinstrument.dao.ShoppingCartDao;
import org.medicalinstrument.impl.CustomerDaoImpl;
import org.medicalinstrument.impl.ShoppingCartDaoImpl;

import java.sql.SQLException;
import java.util.List;


/**
 *@Author cirly
 *@Description 购物车业务逻辑层
 *@Date 20:30 2019/3/17
 *@Modify by 
 *@Version 1.0
 */
public class ShoppingCartService {
    ShoppingCartDao sd = new ShoppingCartDaoImpl();
    //String returnString=null;
/**
 *@Author cirly
 *@Description get users' shoppingCart
 *@Date 19:42 2019/3/18
 *@Modify by
 *@Version 1.0
 */
    public String customerShopping (String accountNo) throws SQLException {
        //customer = ci.customerLogin(custNo, pass);
        List<ShoppingCart_VO> list = null;
        list = sd.getAll(accountNo);
       // System.out.println(list.size());
        if(list != null && list.size() >0){
           return  JSONArray.fromObject(list).toString();
        }else{
           return  "shoppingCart_null";
            //System.out.println(returnString);
        }
        //String result=null;

       // return result;
    }
    /**
     *@Author cirly
     *@Description TO insert a shoppingCart information
     *@Date 19:38 2019/3/19
     *@Modify by
     */
    public String insertShoppingCart(ShoppingCart cart) throws SQLException {
        boolean flag = false;
        flag = sd.add(cart);
        if(flag){
            return "insert_success";
        }else{
            return "insert_error";
        }
    }

    /**
     *@Author cirly
     *@Description work before insert into table 'shoppingcart'
     *@Date 13:10 2019/3/24
     *@Modify by
     *@Version 1.0
     */

    public String isExist (String productQR) throws SQLException {
        ShoppingCart_VO cart = sd.isExist(productQR);
        if(null == cart){
            return "non-existent";
        }else{
            return "exist";
        }
    }
    /**
     *@Author cirly
     *@Description delete shoppingcart
     *@Date 21:44 2019/3/24
     *@Modify by
     *@Version 1.0
     */
    public String delete(List qrList) throws SQLException{
        if(sd.delete(qrList)){
            return "delete_success";
        }else{
            return "delete_error";
        }
    }

    /**
     *@Author cirly
     *@Description update buyNumber after the number of product is added in the shopping cart
     *@Date 16:47 2019/3/26
     *@Modify by
     *@Version 1.0
     */
    public String updateShopping(ShoppingCart cart) throws SQLException{
        if(sd.update(cart)){
            return "update_success";
        }else{
            return "update_error";
        }
    }
}
